/*
 * @Descripttion:
 * @version: 1.0.0
 * @Date: 2021-04-09 14:16:42
 * @LastEditTime: 2022-03-24 14:53:18
 */
import { defined, JulianDate, Viewer, AnimationViewModel } from "cesium";

/**
 * @descripttion: 时间系统
 */
class SystemTime {
  private viewer: Viewer;

  /**
   * @descripttion: 时间管理器
   * @param {*} viewer cesium视图管理器
   */
  constructor(viewer: Viewer) {
    this.viewer = this.init(viewer);
  }

  /**
   * @descripttion: 初始化
   * @param {Viewer} viewer 视图
   * @return {Viewer} 视图
   */
  private init(viewer: Viewer) {
    if (!defined(viewer)) throw new Error("EventListener,无效的视图对象！");
    return viewer;
  }

  /**
   * @descripttion:
   * @param {*} date 时间
   * @param {*} viewModel 视图
   * @param {*} ignoredate 忽略日期
   * @return {String} null
   */
  private DateTimeFormatter(
    date: JulianDate,
    viewModel: AnimationViewModel,
    ignoredate?: boolean
  ) {
    const gregorianDT = JulianDate.toGregorianDate(date);
    let objDT;
    if (ignoredate) objDT = "";
    else {
      objDT = new Date(
        gregorianDT.year,
        gregorianDT.month - 1,
        gregorianDT.day
      );
      objDT =
        gregorianDT.year + "/" + gregorianDT.month + "/" + gregorianDT.day + "";
      if (viewModel || gregorianDT.hour + gregorianDT.minute === 0)
        return objDT;
      objDT += " ";
    }
    // 格式化补零
    function prefixInteger(num: number, length: number) {
      return (Array(length).join("0") + num).slice(-length);
    }
    return (
      objDT +
      `${prefixInteger(gregorianDT.hour, 2)}:${prefixInteger(
        gregorianDT.minute,
        2
      )}:${prefixInteger(gregorianDT.second, 2)}`
    );
  }

  /**
   * @descripttion:
   * @param {*} time 时间
   * @param {*} viewModel 视图
   * @return {String} 时间串
   */
  TimeFormatter = (time: JulianDate, viewModel: AnimationViewModel) =>
    this.DateTimeFormatter(time, viewModel, true);

  /**
   * @descripttion: 初始化本地时间
   * @return {void}
   */
  initLocaleTime() {
    //格式化显示时间
    this.viewer.animation.viewModel.dateFormatter = this.DateTimeFormatter;
    this.viewer.animation.viewModel.timeFormatter = this.TimeFormatter;
    this.viewer.timeline[`makeLabel`] = this.DateTimeFormatter;
  }

  /**
   * @descripttion: 改变时间速率
   * @param {Number} speed 速率
   * @return {void}
   */
  updateTimeSpeed(speed: number) {
    if (Number(speed)) this.viewer.clock.multiplier = speed;
  }

  /**
   * @descripttion: 设置为当前时间
   * @return {void}
   */
  updateTimeToNow() {
    //恢复正常时间
    const start = JulianDate.fromDate(new Date());
    //设置始时钟始时间
    this.viewer.clock.startTime = start.clone();
    //设置时钟当前时间
    this.viewer.clock.currentTime = start.clone();
    //时间恢复
    this.viewer.clock.canAnimate = true; //时间轴
    this.viewer.clock.shouldAnimate = true; //时间轴
  }

  /**
   * @descripttion: 更新当前时间
   * @param {JulianDate} time
   * @return {null}
   */
  updateCurrentTime(time: JulianDate) {
    //设置时钟当前时间
    this.viewer.clock.currentTime = time.clone();
  }

  /**
   * @descripttion: 隐藏时间控件
   * @param {*}
   * @return {*}
   */
  hiddenTimeWidget() {
    //时间控件隐藏
    const creditContainer = this.viewer.cesiumWidget
      .creditContainer as HTMLElement;
    creditContainer.style.display = "none";
    //动画控件隐藏
    const animationContainer = this.viewer.animation.container as HTMLElement;
    animationContainer.style.visibility = "hidden";
    //时间轴控件隐藏
    const timelineContainer = this.viewer.timeline.container as HTMLElement;
    timelineContainer.style.visibility = "hidden";
  }
}
export default SystemTime;
